package com.aikg.kgrag_java.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.aikg.kgrag_java.common.Result;
import com.aikg.kgrag_java.pojo.entity.FileStorage;
import com.aikg.kgrag_java.pojo.entity.Knowledgedb;
import com.aikg.kgrag_java.pojo.vo.FileVo;
import com.aikg.kgrag_java.service.impl.KnowledgeDBServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.ibatis.javassist.runtime.Inner;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import retrofit2.http.GET;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping()
public class KnowledgeDBController {
    @Resource
    private KnowledgeDBServiceImpl knowledgeDBService;

    /**
     * 向指定 projectId 下新增知识库
     *
     * @param projectId 项目ID
     * @param knowledgeDB_name 知识库名称
     * @return HTTP响应
     */
    @PostMapping("/createKnowledgeDB")
    public Result createKnowledgeDBApi(@RequestParam int projectId, @RequestParam String knowledgeDB_name) {
        if (StrUtil.isBlank(knowledgeDB_name)) {
            return Result.buildErrorResult("知识库名称不能为空");
        }
        // TODO 应该在新建项目的时候到es建立索引库，用于向量存储
        boolean knowledgeDB = knowledgeDBService.createKnowledgeDB(projectId, knowledgeDB_name);

        if (knowledgeDB) {
            Map<String,String> map = new HashMap();
            map.put("知识库",knowledgeDB_name);
            return Result.buildSuccessResultWithData(map);
        } else {
            return Result.buildErrorResult("知识库创建失败");
        }
    }

    /**
     * 获取某projectID下的所有知识库
     * @param projectId 项目ID
     * @return
     */
    @GetMapping("/getKnowledgeDBs")
    public Result getKnowledgeDBs(@RequestParam Integer projectId) {
        // 参数校验
        if (projectId == null) {
            return Result.buildErrorResult("获取知识库失败");
        }

        // 获取知识库列表
        List<Knowledgedb> knowledgedbs = knowledgeDBService.list(new QueryWrapper<Knowledgedb>().eq("project_id", projectId));

        // 如果没有找到知识库，直接返回空的列表
        if (knowledgedbs.isEmpty()) {
            return Result.buildSuccessResultWithData(Collections.emptyList());
        }

        // 使用Stream API简化数据处理
        List<List<Object>> knowledgedbsList = knowledgedbs.stream()
                .map(knowledgedb -> {
                    List<Object> list = new ArrayList<>();
                    list.add(knowledgedb.getId());
                    list.add(knowledgedb.getName());
                    return list;
                })
                .collect(Collectors.toList());

        return Result.buildSuccessResultWithData(knowledgedbsList);
    }

    /**
     * 获取某project下的knowledgedb总数
     */
    @GetMapping("/get_knowledgedbs_number")
    public Result getKnowledgedbsNumber(@RequestParam int projectId) {
        return Result.buildSuccessResultWithData(knowledgeDBService.count(
                new QueryWrapper<Knowledgedb>().eq("project_id", projectId))
        );
    }

    /**
     * 删除知识库
     * @param projectId
     * @param knowledgeDB
     * @return
     */
    @PostMapping("/deleteKnowledgeDB")
    public Result deleteKnowledgeDB(@RequestParam int projectId, @RequestParam String knowledgeDB) {
        boolean b = knowledgeDBService.deleteKonwledgeDB(projectId, knowledgeDB);
        if (b) {
            Map<String,String> map = new HashMap();
            map.put("知识库",knowledgeDB);
            return Result.buildSuccessResultWithData(map);
        } else {
            return Result.buildErrorResult("知识库删除失败");
        }
    }
}
